package com.baili.controller.inspector;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baili.dto.BaseTransferEntity;
import com.baili.dto.Call;
import com.baili.dto.Check;
import com.baili.dto.Defect;
import com.baili.dto.ProductOrder;
import com.baili.dto.Sampling;
import com.baili.dto.common.UserConstants;
import com.baili.service.admin.AdminService;
import com.baili.service.inspector.InspectorService;
import com.baili.service.operater.OperaterService;
import com.baili.service.planner.PlannerService;
import com.baili.service.productionorder.ProductionOrderService;
import com.baili.service.user.UserService;
import com.baili.util.EhcacheUtil;
import com.baili.util.MobileConfig;
import com.baili.util.StringUtil;

/**
 * @author iceX
 * @version 创建时间：2016年3月30日 下午2:19:29
 * @Name: InspectorController.java 检验人员
 */
@Controller
@RequestMapping(value = "")
public class InspectorController {

	private static Logger log = Logger.getLogger(InspectorController.class);
	@Autowired
	private InspectorService inspectorService;
	@Autowired
	private PlannerService plannerService;
	@Autowired
	private OperaterService operaterService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private UserService userService;
	@Autowired
	private EhcacheUtil ehcacheUtil;
	@Autowired
	private ProductionOrderService productionOrderService;

	/**
	 * 系统管理员-呼叫设置-呼叫查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallTypeAndCharge")
	@ResponseBody
	public BaseTransferEntity getCallTypeAndCharge(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			// 获取列表数据
			List<Map<String,Object>> lst = operaterService.getEventType();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
//			//baseTransferEntity.setTotalCount("");
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getProductionOrderList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员-缺陷设置-缺陷查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getDefectAndProcess")
	@ResponseBody
	public BaseTransferEntity getDefectAndProcess(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			List<Map<String,Object>> lst = inspectorService.getDefectAndProcess();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
//				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			//baseTransferEntity.setTotalCount("");
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("getProductionOrderList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 检验人员-现场呼叫-事件主题查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getEventType")
	@ResponseBody
	public BaseTransferEntity getEventType(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			Call call = new Call();
			
			String userId = request.getParameter("userId");
			
			call.setUserId(Integer.parseInt(userId));
			// 获取列表数据
			List<Map<String,Object>> lst = operaterService.getCallTypeByUser(call);
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			//baseTransferEntity.setTotalCount("");
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getProductionOrderList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 检验人员-现场呼叫-紧急呼叫查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getcallTypeList")
	@ResponseBody
	public BaseTransferEntity getcallTypeList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			// 获取列表数据
			List lst = inspectorService.getcallTypeList();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			//baseTransferEntity.setTotalCount("");
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getcallTypeList 紧急呼叫--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 检验人员-现场呼叫-提交事件
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/addEventType")
	@ResponseBody
	public BaseTransferEntity addEventType(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
			String eventTopicId = request.getParameter("eventTopicId");
			
			String eventDescription = request.getParameter("eventDescription");
			
			Date callTime = new Date();
			
			Call call = new Call();
			
			String userid = request.getParameter("userId");
			
			String picVdoIds = request.getParameter("picVdoIds");

			String fileName = "";
			// MultipartFile是对当前上传的文件的封装，当要同时上传多个文件时，可以给定多个MultipartFile参数(数组)
			
			if(!StringUtil.isEmpty(picVdoIds)){
				
				fileName = adminService.uploadfiles(userid,picVdoIds);
				
				ehcacheUtil.removeCacheByKey(userid);
			}
			
			call.setCallPersonId(Integer.parseInt(userid));
			
			call.setCallTime(sdf.format(callTime));
			
			call.setCount(0);
			
			call.setDealType(0);
			
			call.setEventDescription(eventDescription);
			
			call.setEventTopicId(Integer.parseInt(eventTopicId));
			
			call.setOverPersonId(0);
			
			call.setPictureName(fileName);
			
			call.setUserId(Integer.parseInt(userid));
			
			call.setViewName(fileName);
			//插入
			operaterService.insertEventType(call);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("addEventType 提交事件--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		return baseTransferEntity;
	}

	/**
	 * 检验人员-现场呼叫-缺陷统计查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getmyFaultList")
	@ResponseBody
	public BaseTransferEntity getmyFaultList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			ProductOrder productOrder = new ProductOrder();

			String processId = request.getParameter("processId");

			productOrder.setUserId(Integer.parseInt(processId));

			// 获取列表数据
			List lst = inspectorService.getmyFaultList(productOrder);

			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("getCallTypeList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 检验人员-现场呼叫-根据时间缺陷统计查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getmyFaultListByTime")
	@ResponseBody
	public BaseTransferEntity getmyFaultListByTime(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("overTime");
			String userId = request.getParameter("userId");
			String productCode = request.getParameter("productCode");
			
			Date overTimeDateAdd = null;
			
			if(!StringUtil.isEmpty(endTime)){
				
				Date overTimeDate = StringUtil.parseDate2(endTime, "yyyy-MM-dd");
				
				endTime = StringUtil.date2String(StringUtil.addDay(overTimeDate, 1));
			}
			
			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(Integer.parseInt(userId));

			Map<String, Object> map = processNameLst.get(0);

			String processName = map.get("processName").toString();
			
			int checkProcess = 0;
			
			if(processName.equals("开料")){
				
				checkProcess = 1;
				
			}else if(processName.equals("制帮")){
				
				checkProcess = 2;	
				
			}else if(processName.equals("中底")){
				
				checkProcess = 3;	
				
			}else if(processName.equals("成型底")){
				
				checkProcess = 4;
				
			}else if(processName.equals("前段") || processName.equals("中段") || processName.equals("后段")){
				
				checkProcess = 5;				
			}

			// 获取列表数据
			List<Map<String, Object>> lst = inspectorService
					.getmyFaultListByTime(startTime, endTime,
							Integer.parseInt(userId),productCode,checkProcess);

//			int myFaultCount = inspectorService.getmyFaultCountByTime(
//					startTime, endTime, Integer.parseInt(userId),productCode);

			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
//			baseTransferEntity.setTotalCount(Integer.toString(myFaultCount));
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getmyFaultListByTime --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 检验人员-呼叫设置
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallList")
	@ResponseBody
	public BaseTransferEntity getCallLists(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			List lst = inspectorService.getCallList();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getCallTypeList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 检验人员-呼叫设置更新状态
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @return
	 */
	@RequestMapping(value = "/service/updateCheakState")
	@ResponseBody
	public BaseTransferEntity updateCheakState(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 判断是选择还是取消
			String state = request.getParameter("state");
			// 当前用户ID
			String userId = request.getParameter("userId");
			// 当前记录ID
			String id = request.getParameter("id");

			// 0表示呼叫设置1表示缺陷设置
			String type = request.getParameter("type");

			List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
			
			if (type.equals("0")) {

				Call call = new Call();

				call.setCallTypeId(Integer.parseInt(id));

				call.setUserId(Integer.parseInt(userId));

				if (state.equals("1")) {
						
					lst = operaterService.insertCallTypeUser(call);

				} else if (state.equals("0")) {

					lst = operaterService.deleteCallTypeUser(call);
				}
				
				call.setUserId(Integer.parseInt(userId));

				lst = operaterService.getCallTypeUser(call);

			} else if (type.equals("1")) {

				Defect defect = new Defect();

				defect.setDefectId(Integer.parseInt(id));

				defect.setUserId(Integer.parseInt(userId));

				if (state.equals("1")) {

					lst = inspectorService.insertDefectUser(defect);

				} else if (state.equals("0")) {

					lst = inspectorService.deleteDefectUser(defect);
				}
				
				lst = inspectorService.getDefectUser(defect);
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("getCallTypeList --------->" + e.getMessage(), e);
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
		}
		return baseTransferEntity;
	}

	/**
	 * 缺陷设置
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getFaultList")
	@ResponseBody
	public BaseTransferEntity getFaultList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			List lst = inspectorService.getFaultList();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getFaultList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 缺陷的更新是否选择
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @return
	 */
	@RequestMapping(value = "/service/updateFaultState")
	@ResponseBody
	public BaseTransferEntity updateFaultState(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			int userId = Integer.parseInt(request.getParameter("userId"));
			int state = 5;
			int id = Integer.parseInt(request.getParameter("id"));
			int processId = Integer.parseInt(request.getParameter("processId"));
			String productNo = request.getParameter("productCode");
			// 获取列表数据
			if (state >= 0 && id >= 0) {
				
				ProductOrder order = new ProductOrder();
				
				order.setId(id);
				
				order = productionOrderService.getOrderById(order);

				Check check = new Check();

				check.setCheckPersonId(userId);

				check.setProductId(id);

				String dateTime = sdf.format(new Date());

				check.setCheckTime(dateTime);

				check.setDefectId(0);

				check.setProductCode(order.getProductCode());
				
				check.setRealStartTime(order.getRealStartTime());
				
				check.setRealOverTime(order.getRealOverTime());
				
				check.setKzType(order.getKzType());
				
				check.setStage(order.getStage());
				
				check.setOperatorId(order.getUserId());
				
				check.setState(state);
				
				check.setScheduleNo(order.getScheduleNo());
				
				List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(userId);
				
				String processName = "";
				
				if(processNameLst != null && processNameLst.size() > 0){
					
					Map<String, Object> map = processNameLst.get(0);
					
					if(map != null ){
						
						processName = map.get("processName").toString();
						
					}
				}
				
				if(processName.equals("开料")){
					
					check.setCheckProcess(1);
					
				}else if(processName.equals("制帮")){
					
					check.setCheckProcess(2);
					
				}else if(processName.equals("中底")){
					
					check.setCheckProcess(3);
					
				}else if(processName.equals("成型底")){
					
					check.setCheckProcess(4);
					
				}else if(processName.equals("前段")){
					
					check.setCheckProcess(5);
					
					check.setState(2);
					
				}else if(processName.equals("中段")){
					
					check.setCheckProcess(6);
					
					check.setState(2);
					
				}else if(processName.equals("后段")){
					
					check.setCheckProcess(7);
					
					check.setState(5);
				}

				List<Map<String, Object>> lst = inspectorService
						.getCheckId(check);

				String checkId = "";

				if (lst == null || lst.size() <= 0) {

					operaterService.insertCheck(check);
					
					lst = inspectorService.getCheckId(check);
					
					checkId = lst.get(0).get("id").toString();

					check.setId(Integer.parseInt(checkId));

				} else {

					checkId = lst.get(0).get("id").toString();

					check.setId(Integer.parseInt(checkId));

					inspectorService.updateCheckTime(check);
				}

				ProductOrder productOrder = new ProductOrder();

				productOrder.setId(processId);

				int stage = 0;
				
				int kzType = 0;

				if (processName.equals("开料")) {

					kzType = 1;
					
					state = 5;

				} else if (processName.equals("制帮")) {

					state = 5;
					
					kzType = 2;
					
				} 
				
				int i = inspectorService.updateFaultState(state, id,
						Integer.parseInt(checkId), stage,kzType,processName);

				if (i == 0) {
					baseTransferEntity.setIsSuccess(i);
					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.success"));
				} else {
					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.error.list"));
					baseTransferEntity.setDesc(MobileConfig
							.getString("msg.global.error.list"));
					return baseTransferEntity;
				}
			}
		} catch (Exception e) {
			log.error("updateFaultState --------->" + e.getMessage(), e);
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData(null);
		}
		return baseTransferEntity;
	}

	/**
	 * 缺陷的更新是否选择(部分缺陷)
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @return
	 */
	@RequestMapping(value = "/service/updateFaultStateN")
	@ResponseBody
	public BaseTransferEntity updateFaultStateN(HttpServletRequest request,
			HttpServletResponse response) {

		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {

			int userId = Integer.parseInt(request.getParameter("userId"));

			int state = 4;

			int id = Integer.parseInt(request.getParameter("id"));

			String defectDet = request.getParameter("defectDet");

			long startTime = Long.valueOf(request.getParameter("startTime"))
					.longValue();
			long overTime = Long.valueOf(request.getParameter("overTime"))
					.longValue();

			int processId = Integer.parseInt(request.getParameter("processId"));

			String productNo = request.getParameter("productCode");
			
			String productName = request.getParameter("productName");
			
			String picVdoIds = request.getParameter("picVdoIds");
			
			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(userId);

			Map<String, Object> map = processNameLst.get(0);

			String processName = map.get("processName").toString();
			
			// 获取列表数据
			if (state >= 0 && id >= 0) {

				ProductOrder order = new ProductOrder();
				
				order.setId(id);
				
				order = productionOrderService.getOrderById(order);
				
				Check check = new Check();

				check.setCheckPersonId(userId);

				check.setProductId(id);

				String dateTime = sdf.format(new Date());

				check.setCheckTime(dateTime);

				check.setDefectId(0);

				check.setProductCode(order.getProductCode());
				
				check.setRealStartTime(order.getRealStartTime());
				
				check.setRealOverTime(order.getRealOverTime());
				
				check.setKzType(order.getKzType());
				
				check.setStage(order.getStage());
				
				check.setOperatorId(order.getUserId());
				
				check.setState(state);
				
				check.setScheduleNo(order.getScheduleNo());
				
				if(processName.equals("开料")){
					
					check.setCheckProcess(1);
					
				}else if(processName.equals("制帮")){
					
					check.setCheckProcess(2);
					
				}else if(processName.equals("中底")){
					
					check.setCheckProcess(3);
					
				}else if(processName.equals("成型底")){
					
					check.setCheckProcess(4);
					
				}else if(processName.equals("前段")){
					
					check.setCheckProcess(5);
					
					check.setState(2);
					
				}else if(processName.equals("中段")){
					
					check.setCheckProcess(5);
				//	check.setCheckProcess(6);
					
					check.setState(2);
					
				}else if(processName.equals("后段")){
					
					check.setCheckProcess(5);
					//check.setCheckProcess(7);
					
					check.setState(5);
				}
				
				List<Map<String, Object>> lst = inspectorService
						.getCheckId(check);

				String checkId = "";
				
				int checkNum = 0;
				
				if(order.getStage() > 0){
					
					if (lst == null || lst.size() <= 0) {
						
						operaterService.insertCheck(check);
						
						checkNum = 1;
					}
					
				}else{
					
					if (lst == null || lst.size() <= 0) {
							
						operaterService.insertCheck(check);
						
						checkNum = 1;
						
					} else {
						
						checkId = lst.get(0).get("id").toString();
						
						check.setId(Integer.parseInt(checkId));
						
						List<Map<String,Object>> maxCheckNum = inspectorService.getMaxCheckNumber(check);
						
						for(Map<String, Object> maxCheckNumMap : maxCheckNum){
							
							checkNum = Integer.parseInt(maxCheckNumMap.get("maxCheckNum").toString()) + 1;
						}
						
						inspectorService.updateCheckTime(check);
					}
				}

				lst = inspectorService.getCheckId(check);

				checkId = lst.get(0).get("id").toString();
				//构建新的缺陷信息
				Defect defect = new Defect();
				
				defect.setProductId(id);

				defect.setStartTime(sdf.format(startTime));

				defect.setOverTime(sdf.format(overTime));

				defect.setProcessId(processId);

				defect.setUserId(userId);

				defect.setProductNo(order.getProductCode());

				defect.setEndTime(dateTime);

				defect.setCheckId(Integer.parseInt(checkId));
				
				defect.setProductName(productName);
				
				defect.setOperatorId(order.getUserId());
				
				defect.setCheckNum(checkNum);

				if(!StringUtil.isEmpty(picVdoIds)){
					//缺陷图片或视频上传
					String fileName = adminService.uploadfiles(userId+"",picVdoIds);
					//缺陷图片和视频存放地址记录
					defect.setPictureName(fileName);
					
					defect.setViewName(fileName);
				}
				
				ehcacheUtil.removeCacheByKey(userId+"");
				
				if(!StringUtil.isEmpty(defectDet)){
					
					String[] str = defectDet.split(",");
					//获取到页面输入的所有缺陷信息
					for (int j = 0; j < str.length; j++) {
						
						String[] strs = str[j].split("-");
						
						defect.setDefectId(Integer.parseInt(strs[0]));
						
						defect.setCount(Integer.parseInt(strs[1]));
						
						defect.setScrapState(Integer.parseInt(strs[2]));
						//创建新的缺陷信息
						inspectorService.insertDefect(defect);
					}
				}
				
				ProductOrder productOrder = new ProductOrder();

				productOrder.setId(processId);

				int stage = 0;
				
				int kzType = 0;

				if (processName.equals("开料")) {

					kzType = 1;
					
				} else if (processName.equals("制帮")) {

					kzType = 2;
					
				} else if (processName.equals("前段") || processName.equals("中段") ) {

					stage = 1;
					
					state = 2;
				} else if(processName.equals("后段")){
					
					stage = 1;
					
					state = 5;
				}
//				else if (processName.equals("中段")) {
//
//					stage = 2;
//
//				} else if (processName.equals("后段")) {
//
//					stage = 3;
//				}
				int i = 0;
				if(processName.equals("前段") || processName.equals("中段") || processName.equals("后段")){
					int type = 0;
					if (processName.equals("前段") ) {

						type = 1;
						
					} else if(processName.equals("中段") ){
						
						type = 2;
						
					}else if(processName.equals("后段")){
						
						type = 3;
					}
					
					i = inspectorService.updateFaultState(state, id,
							Integer.parseInt(checkId), stage,kzType,processName,type);
				}else{
					
					i = inspectorService.updateFaultState(state, id,
							Integer.parseInt(checkId), stage,kzType,processName);
				}

				if (i == 0) {

					baseTransferEntity.setIsSuccess(i);

					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.success"));
				} else {

					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.error.list"));

					baseTransferEntity.setDesc(MobileConfig
							.getString("msg.global.error.list"));

					return baseTransferEntity;
				}
			}
		} catch (Exception e) {
			log.error("updateFaultState --------->" + e.getMessage(), e);
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData(null);
		}
		return baseTransferEntity;
	}

	/**
	 * 我的缺陷列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getFaultListForme")
	@ResponseBody
	public BaseTransferEntity getFaultListForme(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			List lst = inspectorService.getFaultListForme();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getFaultListForme --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 我的缺陷列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getProcessList")
	@ResponseBody
	public BaseTransferEntity getProcessList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			List lst = inspectorService.getProcessList();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getProcessList --------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 删除缺陷列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @return
	 */
	@RequestMapping(value = "/service/deleteFaultList")
	@ResponseBody
	public BaseTransferEntity deleteFaultList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String id = request.getParameter("id");
			// 获取列表数据
			if (id != null) {
				int i = inspectorService.deleteFaultList(Integer.parseInt(id));
				if (i == 0) {
					baseTransferEntity.setIsSuccess(i);
					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.success"));
				} else {
					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.error.list"));
				}
			}
		} catch (Exception e) {
			log.error("deleteFaultList --------->" + e.getMessage(), e);
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
		}
		return baseTransferEntity;
	}

	/**
	 * 新增我的缺陷列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/insrtFaultForme")
	@ResponseBody
	public BaseTransferEntity insrtFaultForme(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String fault = request.getParameter("fault");
			int state = Integer.parseInt(request.getParameter("state"));
			// 获取列表数据
			int lst = inspectorService.insrtFaultForme(fault, state);
			// 返回列表数据
			if (lst != 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);

		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
		}

		return baseTransferEntity;
	}

	/**
	 * 质检任务——待检
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getproductorderListCheck")
	@ResponseBody
	public BaseTransferEntity getproductorderListCheck(
			HttpServletRequest request, HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {

			String processId = request.getParameter("processId");
			String userId = request.getParameter("userId");

			ProductOrder productOrder = new ProductOrder();
			
			productOrder.setUserId(Integer.parseInt(userId));

			productOrder.setId(Integer.parseInt(processId));

			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(Integer.parseInt(userId));

			Map<String, Object> processNameMap = processNameLst.get(0);

			String processName = processNameMap.get("processName").toString();

			if (processName.equals("开料")) {

				processName = "帮面";

				productOrder.setKzType(1);

			} else if (processName.equals("制帮")) {

				processName = "帮面";

				productOrder.setKzType(2);
			} else {

				productOrder.setKzType(0);

			}

			productOrder.setProcessName(processName);
			
			if(processName.equals("开料")){
				
				productOrder.setCheckProcess(1);
				
			}else if(processName.equals("制帮")){
				
				productOrder.setCheckProcess(2);
				
			}else if(processName.equals("中底")){
				
				productOrder.setCheckProcess(3);
				
			}else if(processName.equals("成型底")){
				
				productOrder.setCheckProcess(4);
				
			}else if(processName.equals("前段")){
				
				productOrder.setCheckProcess(5);
				
			}else if(processName.equals("中段")){
				
				productOrder.setCheckProcess(5);
				
			}else if(processName.equals("后段")){
				
				productOrder.setCheckProcess(5);
				
			}

			if (processName.equals("前段") || processName.equals("中段") || processName.equals("后段")) {
				
				productOrder.setStage(1);
				
				productOrder.setRemarks(processName);
				
				processName = "";
				
				productOrder.setProcessName(processName);
				
			} else {
				productOrder.setStage(0);
			}
			// 获取列表数据
			List<Map<String,Object>> lst = inspectorService.getproductorderListCheck(productOrder);
			
			if(lst != null && lst.size() > 0){
				
				for(Map<String,Object> map : lst){
					
					map.put("processName", processName);
				}
			}
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("质检任务——待检--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 质检任务——已检
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getproductorderListChecked")
	@ResponseBody
	public BaseTransferEntity getproductorderListChecked(
			HttpServletRequest request, HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			String userId = request.getParameter("userId");

			ProductOrder productOrder = new ProductOrder();
			
			productOrder.setUserId(Integer.parseInt(userId));
			
			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(Integer.parseInt(userId));

			Map<String, Object> processNameMap = processNameLst.get(0);

			String processName = processNameMap.get("processName").toString();

			
			productOrder.setProcessName(processName);
			
			if(processName.equals("开料")){
				
				productOrder.setCheckProcess(1);
				
			}else if(processName.equals("制帮")){
				
				productOrder.setCheckProcess(2);
				
			}else if(processName.equals("中底")){
				
				productOrder.setCheckProcess(3);
				
			}else if(processName.equals("成型底")){
				
				productOrder.setCheckProcess(4);
				
			}else if(processName.equals("前段")){
				
				productOrder.setCheckProcess(5);
				
			}else if(processName.equals("中段")){
				
				productOrder.setCheckProcess(5);
				
			}else if(processName.equals("后段")){
				
				productOrder.setCheckProcess(5);
				
			}

			// 获取列表数据
			List lst = inspectorService
					.getproductorderListChecked(productOrder);
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error(
					"getproductorderListChecked 呼叫主题--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 质检任务——已检（详情）
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getproductorderListCheckedDetails")
	@ResponseBody
	public BaseTransferEntity getproductorderListCheckedDetails(
			HttpServletRequest request, HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			// 获取列表数据
			List<Map<String,Object>> lst = inspectorService.getproductorderListCheckedDetails(id);
			
			String userId = request.getParameter("userId");

			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(Integer.parseInt(userId));

			Map<String, Object> processNameMap = processNameLst.get(0);

			String processName = processNameMap.get("processName").toString();
			
			// 0表示图片浏览1表示视频浏览
			List<Map<String, Object>> pList = new ArrayList<Map<String,Object>>();
			
			for(Map<String,Object> map : lst){
				
				pList = inspectorService.getCallOrDefectAndPictureOrVedio(id,0,1);
				
				map.put("imageUrls", pList);
				
				List<Map<String, Object>> vList = new ArrayList<Map<String,Object>>();
				
				vList = inspectorService.getCallOrDefectAndPictureOrVedio(id,1,1);
				
				map.put("videoUrls", vList);
				
				map.put("processName", processName);
				
			}
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error(
					"getproductorderListCheckedDetails 呼叫主题--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 质检任务——部分缺陷提交页
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getDefectList")
	@ResponseBody
	public BaseTransferEntity getDefectList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int processId = Integer.parseInt(request.getParameter("processId"));

			Defect defect = new Defect();

			defect.setProcessId(processId);

			// 获取列表数据
			List<Map<String, Object>> lst = inspectorService
					.getDefectList(defect);
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error(
					"getproductorderListCheckedDetails 部分缺陷提交页--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 缺陷个人订制-查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallOrDefectTypeUser")
	@ResponseBody
	public BaseTransferEntity getCallOrDefectTypeUser(HttpServletRequest request, HttpServletResponse response) {

		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		try {

			// 用户ID
			String userId = request.getParameter("userId");
			// 工序ID
			String processId = request.getParameter("processId");
			// 0表示呼叫设置1表示缺陷设置
			String type = request.getParameter("type");

			if (type.equals("0")) {
				
				Call call = new Call();

				call.setUserId(Integer.parseInt(userId));

				lst = operaterService.getCallTypeUser(call);

			} else if (type.equals("1")) {
				
				Defect defect = new Defect();
				
				defect.setUserId(Integer.parseInt(userId));
				
				defect.setProcessId(Integer.parseInt(processId));

				lst = inspectorService.getDefectUser(defect);
			}
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				
				baseTransferEntity.setData(null);
				
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				
				return baseTransferEntity;
			}
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			
			baseTransferEntity.setData(lst);
			
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	/**
	 * 缺陷个人订制-查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getDefectUserLst")
	@ResponseBody
	public BaseTransferEntity getDefectUserLst(HttpServletRequest request, HttpServletResponse response) {
		
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		try {
			
			// 用户ID
			String userId = request.getParameter("userId");
			// 工序ID
			String processId = request.getParameter("processId");
			// 0表示呼叫设置1表示缺陷设置
			String type = request.getParameter("type");
			
			if (type.equals("0")) {
				
				Call call = new Call();
				
				call.setUserId(Integer.parseInt(userId));
				
				lst = operaterService.getCallTypeUser(call);
				
			} else if (type.equals("1")) {
				
				Defect defect = new Defect();
				
				defect.setUserId(Integer.parseInt(userId));
				
				defect.setProcessId(Integer.parseInt(processId));
				
				lst = inspectorService.getDefectUserLst(defect);
			}
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				
				baseTransferEntity.setData(null);
				
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				
				return baseTransferEntity;
			}
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			
			baseTransferEntity.setData(lst);
			
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 缺陷或呼叫视频或图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallOrDefectAndPicture")
	@ResponseBody
	public BaseTransferEntity getCallOrDefectAndPicture(HttpServletRequest request, HttpServletResponse response) {
		
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		try {
			
			// 呼叫或缺陷ID
			String id = request.getParameter("id");
			// 0表示呼叫1表示缺陷
			String type = request.getParameter("type");
			
			// 0表示图片浏览1表示视频浏览
			lst = inspectorService.getCallOrDefectAndPictureOrVedio(Integer.parseInt(id),0,Integer.parseInt(type));
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				
				baseTransferEntity.setData(null);
				
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				
				return baseTransferEntity;
			}
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			
			baseTransferEntity.setData(lst);
			
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 缺陷或呼叫视频或图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallOrDefectAndVedio")
	@ResponseBody
	public BaseTransferEntity getCallOrDefectAndVedio(HttpServletRequest request, HttpServletResponse response) {
		
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		try {
			
			// 呼叫或缺陷ID
			String id = request.getParameter("id");
			// 0表示呼叫1表示缺陷
			String type = request.getParameter("type");
			
			// 0表示图片浏览1表示视频浏览
			lst = inspectorService.getCallOrDefectAndPictureOrVedio(Integer.parseInt(id),1,Integer.parseInt(type));
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				
				baseTransferEntity.setData(null);
				
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				
				return baseTransferEntity;
			}
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			
			baseTransferEntity.setData(lst);
			
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 质检任务——更新合格数量
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/updateQualifiedCount")
	@ResponseBody
	public BaseTransferEntity updateQualifiedCount(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			int number = Integer.parseInt(request.getParameter("number").toString());
			
			String userId = request.getParameter("userId");
			
			List<Map<String,Object>> processNameLst = userService.getProcessNameByUser(Integer.parseInt(userId));
			
			Map<String, Object> processNameMap = processNameLst.get(0);
			
			String processName = processNameMap.get("processName").toString();
//			计数器
			int count = 0;
			
			ProductOrder productOrder = new ProductOrder();
			
			productOrder.setId(id);
			
			productOrder.setNumber(number);
			
			productOrder.setProcessName(processName);
			//获取返回状态
			Map<String,Object> map = inspectorService.updateQualifiedCount(productOrder);
			
			if(!map.isEmpty()){
				
				if(Integer.parseInt(map.get("reason").toString()) == UserConstants.QUALIFIED_GREATER_THAN_PRODUCT){
					
					baseTransferEntity.setResultcode(MobileConfig
							.getStringCode("code.global.error.quagreatthanpro"));
					baseTransferEntity.setData("");
					baseTransferEntity.setDesc(MobileConfig
							.getString("msg.global.error.quagreatthanpro"));
				}
			}else{
				
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData("");
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 抽检——新建抽检信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/insertSampling")
	@ResponseBody
	public BaseTransferEntity insertSampling(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String styleNo = request.getParameter("styleNo");
			
			int workShop = Integer.parseInt(request.getParameter("workShop").toString());
			
			int productTeamId = Integer.parseInt(request.getParameter("productTeamId").toString());
			
			//1-1,14-1,16-2,19-2
			String checkPostAndqualifieds = request.getParameter("checkPostAndqualified");
			
			int checkPersonId = Integer.parseInt(request.getParameter("userId").toString());
			
			String sampling = StringUtil.genSamplingNum();
			
			String samplingTime = sdf.format(new Date());
			
			Sampling samplingDto = new Sampling();
			
			samplingDto.setStyleNo(styleNo);
			
			samplingDto.setWorkShop(workShop);
			
			samplingDto.setProductTeamId(productTeamId);
			
			samplingDto.setCheckPersonId(checkPersonId);
			
			samplingDto.setSampling(sampling);
			
			samplingDto.setSamplingTime(samplingTime);
			
			String[] checkPostAndqualified = checkPostAndqualifieds.split(",");
			
			for(int i = 0 ; i< checkPostAndqualified.length ; i++){
				
				String[] checkPostOrqualified = checkPostAndqualified[i].split("-");
				
				samplingDto.setCheckPostId(Integer.parseInt(checkPostOrqualified[0]));
				
				samplingDto.setQualified(Integer.parseInt(checkPostOrqualified[1]));
				
				//获取返回状态
				inspectorService.insertSampling(samplingDto);
			}
				
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	
	/**
	 * 抽检——新建抽检的页面信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCreateSamplingInfo")
	@ResponseBody
	public BaseTransferEntity getCreateSamplingInfo(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
				//获取返回状态
			List lst = inspectorService.getCreateSamplingInfo();
				
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 抽检——查看抽检信息列表（条件查询）
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getSamplingLst")
	@ResponseBody
	public BaseTransferEntity getSamplingLst(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String startTime = request.getParameter("startTime");
			
			String overTime = request.getParameter("overTime");
			
			String styleNo = request.getParameter("styleNo");
			
			if(!StringUtil.isEmpty(overTime)){
				
				Date overTimeDate = StringUtil.parseDate2(overTime, "yyyy-MM-dd");
				
				overTime = StringUtil.date2String(StringUtil.addDay(overTimeDate, 1));
			}
			
			Sampling sampling = new Sampling();
			
			sampling.setStartTime(startTime);
			
			sampling.setOverTime(overTime);
			
			sampling.setStyleNo(styleNo);
			
			//获取返回状态
			List lst = inspectorService.getSamplingLst(sampling);
				
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 抽检——抽检的详细信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getSamplingDetails")
	@ResponseBody
	public BaseTransferEntity getSamplingDetails(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String samplinga = request.getParameter("sampling");
			
			Sampling sampling = new Sampling();
			
			sampling.setSampling(samplinga);
			
			//获取返回状态
			List lst = inspectorService.getSamplingDetails(sampling);
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 抽检——获取抽检岗位信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCheckPostLst")
	@ResponseBody
	public BaseTransferEntity getCheckPostLst(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			//获取返回状态
			List lst = inspectorService.getCheckPostLst();
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 抽检——新增抽检岗位信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/insertCheckPost")
	@ResponseBody
	public BaseTransferEntity insertCheckPost(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String checkPost = request.getParameter("checkPost");
			
			Sampling sampling = new Sampling();
			
			sampling.setCheckPost(checkPost);
			
			//获取返回状态
			inspectorService.insertCheckPost(sampling);
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 抽检——删除抽检岗位信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/deleteCheckPost")
	@ResponseBody
	public BaseTransferEntity deleteCheckPost(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String id = request.getParameter("id");
			
			Sampling sampling = new Sampling();
			
			sampling.setId(Integer.parseInt(id));
			
			//获取返回状态
			inspectorService.deleteCheckPost(sampling);
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error(
					"updateQualifiedCount 质检任务——更新合格数量--------->"
							+ e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
}
